﻿/*
 * Programer: JR Padfield
 * Description: Handles loading maps, collision server side.
 * Version: 1
 * Date: 01/04/14 
 */

using System;
using System.Collections.Generic;
using TheWorldLibrary.Worlds;
using TheWorldLibrary.TileEngine;

using GDIBitmap = System.Drawing.Bitmap;
using GDIGraphics = System.Drawing.Graphics;
using GDIGraphicsUnit = System.Drawing.GraphicsUnit;
using GDIImage = System.Drawing.Image;
using GDIRectangle = System.Drawing.Rectangle;
using TheWorldLibrary;
using System.Windows.Forms;
using Microsoft.Xna.Framework;

namespace Server.Maps
{
    public class MapManager
    {
        #region Fields

        public World world;
        public LevelData levelData;
        public TileMap map;
        public MapData mapData;
        public List<ILayer> layers = new List<ILayer>();
        public List<Tileset> tileSets = new List<Tileset>();
        public List<GDIImage> tileSetImages = new List<GDIImage>();
        public List<TilesetData> tileSetData = new List<TilesetData>();
        

        #region XNA Field Region

        const int screenWidth = 1024;
        const int screenHeight = 768;

        static Rectangle ScreenRectangle;

        static Game game;

        #endregion

        #endregion

        #region Properties

        #endregion

        #region Properties

        public World Worlds
        {
            get { return world; }
            set { world = value; }
        }

        #endregion

        #region Constructor
        // private constructor
        private MapManager() { }

        // singleton accessor
        static MapManager instance;
        public static MapManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MapManager();
                    ScreenRectangle = new Rectangle(
                        0,
                        0,
                        screenWidth,
                        screenHeight);
                    game = new Game();
                    
                }
                return instance;
            }
        }

        #endregion

        #region Methods

        public void LoadMap(string mapName = "Starting Level")
        {
            LevelData newLevel = null;
            mapData = null;

            try
            {
                newLevel = XNASerializer.Deserialize<LevelData>(Application.StartupPath + @"\Data\Maps\Levels\" + mapName + ".xml");
                mapData = XNASerializer.Deserialize<MapData>(Application.StartupPath + @"\Data\Maps\Levels\Maps\" + newLevel.MapName + ".xml");
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "Error Loading Maps");
                Console.WriteLine(ex.Message, "Error reading map");
            }

            layers.Clear();

            levelData = newLevel;

            if (mapData != null)
            {
                foreach (MapLayerData data in mapData.Layers)
                {
                    layers.Add(MapLayer.MapDataServer(data));
                }

                map = new TileMap(null, (MapLayer) layers[0]);

                for (int l = 1; l < layers.Count; l++)
                {
                    map.AddLayer(layers[l]);
                }

                Level levels = new Level(map);

                if (Worlds == null)
                {
                    Worlds = new World(game, ScreenRectangle);
                }

                Worlds.Levels.Add(levels);
                Worlds.CurrentLevel = 0;
                Worlds.Levels[Worlds.CurrentLevel].MaxHeight = newLevel.MapHeight;
                Worlds.Levels[Worlds.CurrentLevel].MaxWidth = newLevel.MapWidth;
                Worlds.Levels[Worlds.CurrentLevel].Characters.Add(null);
            }

        }

        #endregion

    }
}
